/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.domain;

import org.hibernate.annotations.Proxy;
import org.xptracker.persistence.Identified;
import org.xptracker.persistence.LastModifiedTracker;

import javax.persistence.*;
import java.util.Date;

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "tasks")
@Proxy(lazy = false)
public class Task extends Identified implements LastModifiedTracker {
  private String summary;
  private String description;
  private Date completedDate;
  private Date lastModified;
  private WikiContent descriptionContent = new WikiContent();
  protected User owner;

  public Task() {
  }

  public Task(String summary, String description, User owner) {
    this.summary = summary;
    this.owner = owner;
    setDescription(description);
  }

  public String getSummary() {
    return summary;
  }

  public void setSummary(String summary) {
    this.summary = summary;
  }

  @Lob
  @Column(length = 65535, name = "description")
  public String getOldDescription() {
    return description;
  }

  public void setOldDescription(String description) {
    this.description = description;
  }

  @Transient
  public String getDescription() {
    return getDescriptionContent() == null ? "" : getDescriptionContent().getContent();
  }

  @Transient
  public void setDescription(String description) {
    if (descriptionContent == null) {
      throw new RuntimeException("WikiContent may not be null!");
    }
    getDescriptionContent().setContent(description);
  }

  public boolean isComplete() {
    return completedDate != null;
  }

  public void setComplete(boolean complete) {
    if (complete) {
      this.completedDate = new Date();
    } else {
      completedDate = null;
    }
  }

  @Column(name = "completed_date")
  public Date getCompletedDate() {
    return completedDate;
  }

  public void setCompletedDate(Date completedDate) {
    this.completedDate = completedDate;
  }

  @Column(name = "modified_date")
  public Date getLastModified() {
    return lastModified;
  }

  public void setLastModified(Date date) {
    this.lastModified = date;
  }

  public String toString() {
    return "Task " + getId() + ": " + summary;
  }

  public boolean equals(Object o) {
    return super.equals(o);
  }

  public int hashCode() {
    return super.hashCode();
  }

  @OneToOne
  public User getOwner() {
    return owner;
  }

  public void setOwner(User owner) {
    this.owner = owner;
  }

  @ManyToOne(cascade = CascadeType.ALL)
  @JoinColumn(name = "description_content_id")
  public WikiContent getDescriptionContent() {
    if (descriptionContent == null) {
      descriptionContent = new WikiContent();
    }
    return descriptionContent;
  }

  public void setDescriptionContent(WikiContent descriptionContent) {
    this.descriptionContent = descriptionContent;
  }
}